
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.JDOMParseException;
import org.jdom.input.SAXBuilder;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ale
 */
public class LoadGame {
    private SAXBuilder builder;
    private Document doc;
    private Element rootElement;
    private List <Player> players=new ArrayList<Player>();
    private int numberphrases;
    private String[] letterString;
    private int playerNumber;
    private String dataString;
    private boolean wheelButton;
    private boolean passButton;
    private boolean jollyButton;

    public LoadGame(){  try {
            builder = new SAXBuilder(true);
             doc = builder.build(new File("../data/partitaSalvata.xml"));
            rootElement = doc.getRootElement();
            
            //children = rootElement.getChildren(); 
            
            //System.out.println(phrases.getName());
        }catch(JDOMParseException ex){
             Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Errore file xml");
            JOptionPane.showMessageDialog(new JPanel(), "errore file xml");
            System.exit(0);
        } catch (JDOMException ex) {
             System.out.println("Errore file xml");
            JOptionPane.showMessageDialog(new JPanel(), "errore file xml");
            
        } catch (IOException ex) {
              System.out.println("Errore file xml");
            JOptionPane.showMessageDialog(new JPanel(), "errore file xml");
            
        }
    }
    public void initializeLoad(){
        Wheel();
        Phrases();
        Players();
        JOptionPane.showMessageDialog(null, "Partita Caricata");
    }
    public void Phrases(){
    Element phrases_el=rootElement.getChild("phrases");
//            List children=phrases_el.getChildren();
          //  for(int i=0; i<children.size();i++){
//                System.out.println(((Element)children.get(i)).getName());
//                System.out.println(((Element)children.get(i)).getChildText("text"));
                numberphrases=Integer.parseInt(phrases_el.getChildText("number").trim());
                letterString=(phrases_el.getChildText("letters").trim().split(","));
                //letters.add
               // phrases.add(new Phrase(((Element)children.get(i)).getChildText("text").trim(),((Element)children.get(i)).getChildText("hint").trim()));
        //    }
//            for(int i=0; i<phrases.size();i++){
//                System.out.println(phrases.get(i).getText());
//                System.out.println(phrases.get(i).getHint());
//            }
}
//public List<Phrase> getPhrases(){
//   // normalize text representation
//     return phrases;
//         
//}
public void Players(){
    
    Element phrases_el=rootElement.getChild("players");
    //  Player player;
            List children=phrases_el.getChildren();
            System.out.println(children.size());
            for(int i=0; i<children.size()-1;i++){
               // System.out.println(((Element)children.get(i)).getName());
              //  System.out.println("nome giocatore trim: "+((Element)children.get(i)).getChildText("name").trim());
                players.add(new Player(((Element)children.get(i)).getChildText("name").trim()));
                System.out.println("caricamento nome giocatore: "+players.get(i).getPlayerName());
                players.get(i).setManchePoint(Integer.parseInt(((Element)children.get(i)).getChildText("manche").trim()));
                 players.get(i).setGamePoint(Integer.parseInt(((Element)children.get(i)).getChildText("gamepoint").trim()));
                 players.get(i).setJollyBoolean(Boolean.parseBoolean(((Element)children.get(i)).getChildText("jolly").trim()));
                 //players.add(player);
                //players.add(((Element)children.get(i)).getChildText("name").trim());
            }
            playerNumber=Integer.parseInt(phrases_el.getChildText("number").trim());
            System.out.println("player number: "+playerNumber);
}
public void Wheel(){
    Element phrases_el=rootElement.getChild("wheel");
//            List children=phrases_el.getChildren();
          //  for(int i=0; i<children.size();i++){
//                System.out.println(((Element)children.get(i)).getName());
//                System.out.println(((Element)children.get(i)).getChildText("text"));
                dataString=phrases_el.getChildText("element").trim();
                 wheelButton=Boolean.parseBoolean(phrases_el.getChildText("wheelButton").trim());
                  passButton=Boolean.parseBoolean(phrases_el.getChildText("passButton").trim());
                   jollyButton=Boolean.parseBoolean(phrases_el.getChildText("jollyButton").trim());
   
                //letters.add
               // phrases.add(new Phrase(((Element)children.get(i)).getChildText("text").trim(),((Element)children.get(i)).getChildText("hint").trim()));
        //    }
//            for(int i=0; i<phrases.size();i++){
//                System.out.println(phrases.get(i).getText());
//                System.out.println(phrases.get(i).getHint());
//            }
}
public List<Player> getPlayers(){
   // normalize text representation
     return players;
         
}
public int getPhrasesNumber(){
   // normalize text representation
     return numberphrases;
         
}
public int getPlayerNumber(){
    return playerNumber;
}
public String[] getLetters(){
   // normalize text representation
     return letterString;
         
}
public boolean getWheelButton(){
    return wheelButton;
}
public boolean getPassButton(){
    return passButton;
}
public boolean getJollyButton(){
    return jollyButton;
}
public String getDataString(){
    return dataString;
}
}
